package keqiang.com.kq_flutter_core_widget.channel;

import android.content.Context;

import androidx.annotation.NonNull;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodChannel;
import keqiang.com.kq_flutter_core_widget.interf.IKqPush;

/**
 * flutter 关闭原生页面通道
 *
 * @author zhouzhuo810
 * @date 2023/3/13 17:49
 */
public class NativePushChannel extends MethodChannel {
    
    // 通道id
    private static final String CHANNEL = "flutter.keqiang.com/push";
    
    private static final String METHOD_TURN_ON_PUSH = "turnOnPushChannel";
    private static final String METHOD_TURN_OFF_PUSH = "turnOffPushChannel";
    private static final String METHOD_SET_ALIAS = "setAlias";
    
    private static final String METHOD_REMOVE_ALIAS = "removeAlias";
    
    private Context mContext;
    
    public NativePushChannel(@NonNull BinaryMessenger messenger) {
        super(messenger, CHANNEL);
        
        init();
    }
    
    
    public void setContext(Context context) {
        mContext = context;
    }
    
    private void init() {
        setMethodCallHandler((call, result) -> {
            switch (call.method) {
                case METHOD_TURN_ON_PUSH:
                    result.success(true);
                    if (mContext != null) {
                        if (mContext instanceof IKqPush) {
                            ((IKqPush) mContext).turnOnPushChannel();
                        }
                    }
                    break;
                case METHOD_TURN_OFF_PUSH:
                    result.success(true);
                    if (mContext != null) {
                        if (mContext instanceof IKqPush) {
                            ((IKqPush) mContext).turnOffPushChannel();
                        }
                    }
                    break;
                case METHOD_SET_ALIAS:
                    result.success(true);
                    if (mContext != null) {
                        if (mContext instanceof IKqPush) {
                            String alias = call.argument("alias");
                            ((IKqPush) mContext).setAlias(alias);
                        }
                    }
                    break;
                case METHOD_REMOVE_ALIAS:
                    result.success(true);
                    if (mContext != null) {
                        if (mContext instanceof IKqPush) {
                            String alias = call.argument("alias");
                            ((IKqPush) mContext).removeAlias(alias);
                        }
                    }
                    break;
            }
        });
    }
    
}
